package io.kiki.sba.registry.metrics;

import io.prometheus.client.Collector;
import io.prometheus.client.CounterMetricFamily;
import io.prometheus.client.SimpleCollector;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

public class CounterFunc extends SimpleCollector<CounterFunc.Child> implements Collector.Describable {

    CounterFunc(Builder b) {
        super(b);
    }

    public static Builder build() {
        return new Builder();
    }

    @Override
    public List<MetricFamilySamples> collect() {
        List<MetricFamilySamples.Sample> samples = new ArrayList<>(children.size());
        for (Map.Entry<List<String>, CounterFunc.Child> c : children.entrySet()) {
            samples.add(new MetricFamilySamples.Sample(fullname, labelNames, c.getKey(), c.getValue().get()));
        }
        return familySamplesList(Type.COUNTER, samples);
    }

    @Override
    public List<MetricFamilySamples> describe() {
        return Collections.<MetricFamilySamples>singletonList(new CounterMetricFamily(fullname, help, labelNames));
    }

    @Override
    protected Child newChild() {
        return new Child();
    }

    public synchronized CounterFunc func(CounterFuncCallable c) {
        noLabelsChild.func(c);
        return this;
    }

    public interface CounterFuncCallable {
        long get();
    }

    public static class Builder extends SimpleCollector.Builder<Builder, CounterFunc> {

        @Override
        public CounterFunc create() {
            return new CounterFunc(this);
        }
    }

    public static class Child {
        private CounterFuncCallable callable;

        public synchronized Child func(CounterFuncCallable c) {
            callable = c;
            return this;
        }

        public double get() {
            return callable.get();
        }
    }
}
